<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="脚本名称" prop="scriptName">
          <el-input
            v-model="queryParams.scriptName"
            placeholder="请输入脚本名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['job:taskScript:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['job:taskScript:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['job:taskScript:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['job:taskScript:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskScriptList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="脚本ID" width="55" align="center" prop="scriptId" />
      <el-table-column label="脚本名称" align="center" prop="scriptName"  />
      <el-table-column label="脚本文件名" align="center" prop="scriptFileName"   />
      <el-table-column label="部署状态" width="75" align="center" prop="delFlag"  :formatter="delFlagFormat" />
      <el-table-column label="脚本描述" align="center" prop="scriptDesc"   />
      <el-table-column label="部署路劲" align="center" prop="pathUrl"   />
      <el-table-column label="部署时间" align="center" prop="deployTime"  />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改Kettle脚本对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="脚本名称">
          <el-input v-model="form.scriptName" placeholder="请输入脚本名称" clearable :style="{ width: '100%' }"></el-input>
        </el-form-item>
        <el-form-item label="脚本类型" prop="scriptType">
          <el-select v-model="form.scriptType" placeholder="请选择脚本类型">
            <el-option
              v-for="dict in scriptTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="脚本描述">
          <el-input v-model="form.scriptDesc" placeholder="请输入脚本描述" clearable :style="{ width: '100%' }"></el-input>
        </el-form-item>
        <el-form-item label="上传脚本文件">
           <el-upload
             ref="upload"
             :limit="1"
             accept=".jpg, .png, .ktr"
             :action="upload.url"
             :headers="upload.headers"
             :file-list="upload.fileList"
             :on-success="handleFileSuccess"
             :auto-upload="false">
             <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
             <el-button style="margin-left: 10px;" size="small" type="success" :loading="upload.isUploading" @click="submitUpload">上传到服务器</el-button>
             <div slot="tip" class="el-upload__tip">只能上传Kettle脚本文件</div>
           </el-upload>

         <!-- <el-upload
            class="upload-demo"
            ref="upload"
            action="http://32.172.33.11:8080/job/taskScript/uploadKettleFile"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :auto-upload="false">
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
            <div slot="tip" class="el-upload__tip">只能上传Kettle脚本文件</div>
          </el-upload> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTaskScript, getTaskScript, delTaskScript, addTaskScript, updateTaskScript } from "@/api/job/taskScript";
import Editor from '@/components/Editor';
import { getToken } from "@/utils/auth";

export default {
  name: "TaskScript",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Kettle脚本表格数据
      taskScriptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // $comment字典
      scriptTypeOptions: [],
      delFlagOptions: [],
      // 上传文件
      fileList: [],
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API +"/schedule/taskScript/uploadKettleFile",
        // 上传的文件列表
        fileList: []
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scriptName: null,
        scriptFileName: null,
        scriptType: null,
        scriptContent: null,
        scriptDesc: null,
        state: null,
        pathUrl: null,
        deployTime: null,
        delFlag: '0',
        remark: null,
        createUser: null,
        createTime: null
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
	this.getDicts("script_type").then(response => {
      this.scriptTypeOptions = response.data;
    });
    this.getDicts("sys_notice_status").then(response => {
      this.delFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询Kettle脚本列表 */
    getList() {
      this.loading = true;
      listTaskScript(this.queryParams).then(response => {
        this.taskScriptList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // $comment字典翻译
    scriptTypeFormat(row, column) {
      return this.selectDictLabel(this.scriptTypeOptions, row.scriptType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scriptId: null,
        scriptName: null,
        scriptFileName: null,
        scriptType: null,
        scriptContent: null,
        scriptDesc: null,
        state: null,
        pathUrl: null,
        deployTime: null,
        delFlag: null,
        remark: null,
        createUser: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scriptId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增Kettle脚本";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const scriptId = row.scriptId || this.ids
      getTaskScript(scriptId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改Kettle脚本";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.scriptId != null) {
            updateTaskScript(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTaskScript(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scriptIds = row.scriptId || this.ids;
      this.$confirm('是否确认删除Kettle脚本编号为"' + scriptIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTaskScript(scriptIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('job/taskScript/export', {
        ...this.queryParams
      }, `job_taskScript.xlsx`)
    },
    delFlagFormat(row, column) {
      return this.selectDictLabel(this.delFlagOptions, row.delFlag);
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.form.pathUrl = response.pathUrl;
      this.msgSuccess(response.msg);
    }
  }
};
</script>
